/*
 * @Description: 单位管理reducer
 * @Author: Rfan
 * @Date: 2022-06-11 09:59:07
 * @LastEditTime: 2022-06-11 15:18:07
 */

import { delItemByIdArray, editItemById, insertItem } from '@/utils/list';
import {
  ADD_CHANGELOG,
  DELETE_CHANGELOG,
  EDIT_CHANGELOG,
  HIDE_LOADING,
  LOADING,
  SET_CHANGELOG_LIST
} from '../constants';

export const ChangeLogReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 表格加载loading
     */
    case LOADING:
      return { ...state, loading: true };

    /**
     * @description: 隐藏加载loading
     */
    case HIDE_LOADING:
      return { ...state, loading: false };

    /**
     * @description: 加载表格数据及相关数据
     * @param {Array} data 表格数据
     * @param {number} TotalCount 数据总数量
     * @return {*}
     */
    case SET_CHANGELOG_LIST: {
      return {
        ...state,
        list: action.data,
        loading: false
      };
    }

    /**
     * @description: 添加单位
     * @param {Object} data 新增的全宗
     */
    case ADD_CHANGELOG:
      return {
        ...state,
        list: insertItem(action.data, state.list),
        dialog: { visible: false }
      };

    /**
     * @description: 编辑单位
     * @param {number}  编辑的单位ID
     * @param {IUnit} data 编辑的单位
     */
    case EDIT_CHANGELOG:
      return {
        ...state,
        list: editItemById(action.id, action.data, state.list)
      };

    /**
     * @description: 删除单位
     * @param {number[]} ids 删除全宗的ID
     */
    case DELETE_CHANGELOG: {
      const list = delItemByIdArray(action.ids, state.list);
      return { ...state, list };
    }

    default:
      console.log('untouched', action);
      return state;
  }
};
